package com.yanqu.road.server.manager.union;

import com.yanqu.road.entity.enums.eRankType;
import com.yanqu.road.entity.rank.UnionRank;
import com.yanqu.road.entity.servercenter.ServerGroup;
import com.yanqu.road.entity.union.cross.CrossUnionInfo;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossGroupUnionRankListModel;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.union.unionrank.CrossUnionRankMsgCacheMgr;
import com.yanqu.road.server.manger.ServerGroupMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.RandomHelper;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossUnionRankMgr extends TempCrossMgr {

    @Override
    public boolean removeExpireData() throws Exception {
        return false;
    }

    @Override
    public boolean init() throws Exception {
        addServerGroupReloadListener();
        reloadData();
        return true;
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        reloadGroupRank();
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    private void addServerGroupReloadListener(){
        ServerGroupMgr.addReloadListener(() -> {
            reloadGroupRank();
        });
    }

    private static void reloadGroupRank(){
        initCrossGroupUnionRankListModelMap();
    }

    private static void initCrossGroupUnionRankListModelMap(){
        List<ServerGroup> serverGroupList = ServerGroupMgr.getServerGroupList(ConfigHelper.getInt("channelId"));
        for (ServerGroup serverGroup : serverGroupList) {
            CrossUnionRankMsgCacheMgr.getUnionCrossRankMsgByGroupId(serverGroup.getGroupId());
        }
    }

    /**
     * 跨服膜拜
     * @param req
     * @return
     */
    public static RankProto.CrossGroupWorshipRespMsg.Builder rankWorship(RankProto.CrossGroupWorshipReqMsg req){
        RankProto.CrossGroupWorshipRespMsg.Builder builder = RankProto.CrossGroupWorshipRespMsg.newBuilder();
        int groupId = req.getGroupId();
        int rankType = req.getCorssType();
        int gameType = req.getGameType();
        int bigType = req.getBigRankType();
        builder.setType(gameType);
        if (eRankType.CrossUnionEarnSpeed.getValue() == rankType) {
            RankProto.UnionRankListMsg listMsg = CrossUnionRankMsgCacheMgr.getUnionCrossRankMsgByGroupId(groupId);
            if(null == listMsg || listMsg.getRankListCount() <= 0){
                builder.setRet(GameErrorCode.E_RANK_WORSHIP_NO_DATA);
                return builder;
            }
            RandomHelper randomHelper = new RandomHelper();
            int next = randomHelper.next(0, listMsg.getRankListCount());
            RankProto.UnionRankTempMsg unionRankMsg = listMsg.getRankList(next);
            if(unionRankMsg != null){
                builder.setUnionRankData(unionRankMsg);
            }
            int rankNum = next + 1;
            builder.setRankNum(rankNum);
        }
        builder.setRet(0);
        return builder;
    }
}
